﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyMovieInfo
{
    class MovieListFromFileNames : IMovieListCreator
    {
        const string movieregex = @"^(?<path>.*\\)(?<name>.+?)\((?<year>\d{4})\)\.(?<ext>\w{3})$";

        private List<string> _filenames = new List<string>();

        public MovieListFromFileNames(List<string> filenames)
        {
            this._filenames = filenames;
        }


        public List<Movie> CreateMovieQueries()
        {
            List<Movie> movieQueries = new List<Movie>();

            foreach (var moviefile in _filenames)
            {
                var query = new Movie() { FilePath = moviefile, MatchFound=false };

                //TODO: Use compiled regex
                MatchCollection matches = Regex.Matches(moviefile, movieregex, RegexOptions.IgnoreCase);

                if (matches != null && matches.Count > 0)
                {
                    string moviename = matches[0].Groups["name"].Value.Trim();
                    //TODO: Add Error handling or TryParse
                    int movieyear = Int32.Parse(matches[0].Groups["year"].Value);

                    query.Name = moviename;
                    query.Year = movieyear;

                    movieQueries.Add(query);
                }
            }

            return movieQueries;
        }
    }
}
